<?php

namespace App\Admin\Controllers;

use App;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Models\WechatUser;
use WechatUserService;

class WechatUserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.wechat_user.title.list'));
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('backend.wechat_user.title.edit'));
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.wechat_user.title.add'));
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(WechatUser::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->uid(trans('backend.wechat_user.column.uid'));
            $grid->openid(trans('backend.wechat_user.column.openid'));
            $grid->nick_name(trans('backend.wechat_user.column.nickname'));
            $grid->sex(trans('backend.wechat_user.column.sex'))->value(function($sex){
                return $sex>0?'男':'女';
            });
            $grid->province(trans('backend.wechat_user.column.province'));
            $grid->city(trans('backend.wechat_user.column.city'));
            $grid->country(trans('backend.wechat_user.column.country'));
//            $grid->headimgurl('headimgurl')->image();;
            $grid->union_code(trans('backend.wechat_user.column.union_code'))->value(function($unionCode){
                return $unionCode;
            });
            $grid->from_url(trans('backend.wechat_user.column.from_url'));
            $grid->disableCreation();

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(WechatUser::class, function (Form $form) {
            $form->display("uid",trans('backend.wechat_user.column.uid'));
            $form->display("openid",trans('backend.wechat_user.column.openid'));
            $form->display("nickname",trans('backend.wechat_user.column.nickname'));
            $form->display("sex",trans('backend.wechat_user.column.sex'));
            $form->display("province",trans('backend.wechat_user.column.province'));
            $form->display("city",trans('backend.wechat_user.column.city'));
            $form->display("country",trans('backend.wechat_user.column.country'));
            $form->display("headimgurl",trans('backend.wechat_user.column.headimgurl'));
            $form->display("union_code",trans('backend.wechat_user.column.union_code'));
            $form->display("from_url",trans('backend.wechat_user.column.from_url'));

        });
    }

    public function options()
    {
        return WechatUserService::getResultOptions();
    }
}
